home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw GX / Programming Stuff / Sample Code / Printing Samples / Applications… / All Shapes with Printing ƒ / graphics shell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-03-20  |  8.4 KB  |  299 lines  |  [TEXT/KAHL]

  1. /**
  2.  --        Graphics Shell.c (modified)
  3.  --
  4.  --        This file is a shell that can be used to build "new" Graphics applications.  
  5.  --        It contains all of the required calls to use the "new" Graphics routines and 
  6.  --        QuickDraw (i.e. windows) together. It will put up one window. You "quit" 
  7.  --        the application by clicking in the close bix.  This shell does not use a menu.
  8.  --        
  9.  --     The application is expected to supply the following functions which are called
  10.  --        by this shell:
  11.  --
  12.  --            void DoInitialization(WindowPtr);
  13.  --            void DoDraw(WindowPtr);
  14.  --            void DoDispose(WindowPtr);
  15.  --            void DoClick(WindowPtr);
  16.  --            void DoIdle(WindowPtr);
  17.  --
  18.  --
  19.  --     Change History:         
  20.  --
  21.  --        3/90    New
  22.  --        6/91    Updated the shell to reflect the changes in "Graphics" v1.0d21.2.  PLA
  23.  --        6/92    Made the following variables global: gDebugging, gGiveMeValidation,gGraphicsHeapSize. 
  24.  --                See the comments for detail in this file.
  25.  --        6/92    Added printing guts, mangled and reorganized.  DMH
  26.  --        9/93    Updated files to work with the ß2 "GXified" interface files.   PLA
  27.  --        9/93    Added override for gxPrintingEvent to handle update events.       DMH
  28.  --        9/93    Added GXUpdateJob for resume events.    DMH
  29.  --        5/94    gxPrintingEvent override now ignores appropriate events. DMH
  30.  --        8/94    Universalized. DMH
  31.  --
  32.  --        © Apple Computer, Inc. 1990 - 1993  All rights reserved
  33.  --                                
  34. **/
  35.  
  36.  
  37. #include <Desk.h>
  38. #include <Events.h>
  39. #include <Fonts.h>
  40. #include <Windows.h>
  41. #include <Memory.h>
  42. #include <ToolUtils.h>
  43. #include <Menus.h>
  44. #include <Quickdraw.h>
  45.  
  46. #include <GXPrinting.h>
  47. #include <GXEnvironment.h>
  48. #include <GXGraphics.h>
  49. #include "GraphicsLibraries.h"
  50. #include <GXErrors.h>
  51. #include "graphics shell.h"
  52.  
  53. #define    kOSEvent                        app4Evt    /* event used by MultiFinder */
  54. #define    kSuspendResumeMessage            1        /* high byte of suspend/resume event message */
  55. #define    kResumeMask                        1        /* bit of message field for resume vs. suspend */
  56.  
  57. Boolean            gQuitting;
  58. long            gSleep = 0;
  59.  
  60. /*------ main -----------------------------------------------------------------------------------------*/
  61.  
  62. void main()
  63. {        
  64.     CursHandle            theCurs; 
  65.     Handle                menuBar;
  66.     gxGraphicsClient     client;
  67.     OSErr                err;
  68.     WindowPtr            wind;
  69.     
  70.     /**     
  71.         The GXNewGraphicsClient routine defines the graphics heap size. If you do not make this call, 
  72.         the GX graphics engine will create this heap automatically. How? It will create a heap which 
  73.         is a percentage of your application's ideal memory foot print. This call allows you to explicity 
  74.         define the ammount of memory used by the graphics system for it's graphics objects heap.
  75.     **/
  76.     client = GXNewGraphicsClient(nil, gGraphicsHeapSize * 1024, 0L);
  77.  
  78.     /**   Generic heap initialization.  **/
  79.     MaxApplZone(); 
  80.     MoreMasters(); MoreMasters(); MoreMasters(); 
  81.     MoreMasters(); MoreMasters(); MoreMasters(); 
  82.  
  83.     /** 
  84.         If gDebugging = TRUE, you will receive graphics library errors & notices will be posted.  This
  85.         functionality will only work with the "debugging" version of the QuickDraw GX init.  If this
  86.         init is not installed, these functions will not work. 
  87.     **/
  88.     if (gDebugging) {
  89.         SetGraphicsLibraryErrors ();
  90.         SetGraphicsLibraryNotices();    
  91.     }
  92.  
  93.     /** 
  94.         Set  "gGiveMeValidation" to TRUE, if you want run-time validation. As you increase the amount
  95.         of validation, The drawing speed will SLOW down due to all of the internal checking. 
  96.         
  97.         gxPublicValidation will check parameters to public routines. For additional details regarding 
  98.         the various levels of validation, please see the documentation.
  99.     **/
  100.     if (gGiveMeValidation) GXSetValidation(gxPublicValidation); 
  101.  
  102.  
  103.     /** initialize the new graphics and printing environments.  **/
  104.  
  105.     GXEnterGraphics();
  106.     err = GXInitPrinting();
  107.  
  108.     /**   initialize the other managers, if no errors occured.   **/
  109.      
  110.      if (!err)
  111.      {
  112.          InitGraf(&qd.thePort);
  113.         InitFonts();
  114.         InitWindows();
  115.         InitMenus();
  116.         InitCursor();
  117.  
  118.         theCurs = GetCursor(watchCursor);
  119.         SetCursor(*theCurs);
  120.  
  121.         menuBar = GetNewMBar(rMenuBar); /* Create the menu bar */
  122.  
  123.         if (menuBar)
  124.         {
  125.             gQuitting = false;
  126.             SetMenuBar(menuBar);
  127.             DisposHandle(menuBar);
  128.             AddResMenu(GetMHandle(mApple), 'DRVR'); /* add Apple Menu items */
  129.             DrawMenuBar();
  130.  
  131.      // We gxInitialize the CommonColors Library.  This will allow us to set the color of a
  132.      // shape by calling the SetShapeCommonColor function. We will need to call
  133.      // DisposeCommonColors when we leave, to clean up the world.
  134.  
  135.               InitCommonColors();
  136.     
  137.             DoCreateNew();
  138.             SetCursor(&qd.arrow);  
  139.             
  140.             while (!gQuitting)
  141.             {
  142.                 EventLoop();
  143.                 DoIdle(wind);
  144.             }
  145.  
  146.  
  147.     // Leaving.  Close all the windows so we get rid of any data we or GX created.  Then,
  148.     // dispose of the common colors and exit the GX printing and graphics environment.
  149.  
  150.             while (wind = FrontWindow())
  151.                 DoDispose(wind);
  152.  
  153.             DisposeCommonColors();
  154.         }
  155.  
  156.         GXExitPrinting();    /** Close the new printing mgr. **/
  157.     }
  158.     
  159.     GXExitGraphics();        /** Deallocate all of the default structures **/
  160.     GXDisposeGraphicsClient(client);
  161. }
  162.  
  163.  
  164. /*------ MyPrintingEventOverride -----------------------------------------------------------------------------*/
  165. // Override for GXPrintingEvent.  It allows us to update our windows
  166. // when the moveable modal printing dialogs are moved.
  167.  
  168. OSErr MyPrintingEventOverride(EventRecord *anEvent, Boolean filterEvent)
  169. {
  170.     OSErr    err = noErr;
  171.  
  172.     // Handle events in whatever way is appropriate.  MyDoEvent is our
  173.     // generic event handler.  We don't pass it events that it shouldn't
  174.     // handle while print dialogs are displayed.
  175.  
  176.     if (!filterEvent)
  177.         switch (anEvent->what)
  178.         {
  179.             case mouseDown:
  180.             case keyDown:
  181.             case autoKey:
  182.                 break;
  183.             
  184.             default:
  185.                 MyDoEvent(anEvent);
  186.         }
  187.  
  188.     return err;
  189. }
  190.  
  191.  
  192. /*------ EventLoop ------------------------------------------------------------------------------------*/
  193.  
  194. void EventLoop()
  195. {
  196.     EventRecord        theEvent;
  197.  
  198.     if (WaitNextEvent(everyEvent, &theEvent, gSleep, nil))
  199.         MyDoEvent(&theEvent);
  200. }
  201.  
  202.  
  203.  
  204. /*------ MyDoEvent ------------------------------------------------------------------------------------*/
  205.  
  206. void MyDoEvent(EventRecord *theEvent)
  207. {
  208.     char            key;
  209.     WindowPtr         whichWindow;
  210.     GrafPtr            oldPort;
  211.  
  212.     switch(theEvent->what)
  213.     {                    
  214.         case updateEvt:
  215.             if (((WindowPeek) theEvent->message)->windowKind == userKind)
  216.             {
  217.                 GetPort(&oldPort);
  218.                 SetPort((WindowPtr) theEvent->message);
  219.                 BeginUpdate((WindowPtr) theEvent->message);
  220.                 DoDraw((WindowPtr) theEvent->message);
  221.                 EndUpdate((WindowPtr) theEvent->message);
  222.                 SetPort(oldPort);
  223.               }
  224.         break;
  225.         
  226.         case mouseDown:
  227.         switch (FindWindow(theEvent->where, &whichWindow)) {
  228.             case inSysWindow:
  229.                 SystemClick(theEvent, whichWindow);
  230.             break;
  231.                     
  232.             case inDrag:
  233.                 if (((WindowPeek) whichWindow)->windowKind == userKind)
  234.                     DragWindow(whichWindow, theEvent->where, &qd.screenBits.bounds);
  235.             break;
  236.  
  237.             case inGoAway:
  238.                 if (((WindowPeek) whichWindow)->windowKind == userKind)
  239.                 {
  240.                     if (TrackGoAway(whichWindow, theEvent->where))
  241.                         DoDispose(whichWindow);
  242.                 }
  243.             break;
  244.             
  245.             case inContent:
  246.                 if ((whichWindow != FrontWindow()) &&
  247.                     (((WindowPeek) whichWindow)->windowKind == userKind))
  248.                      SelectWindow(whichWindow);
  249.             break;
  250.             case inMenuBar:
  251.                 DoMenuCommand(MenuSelect(theEvent->where));
  252.             break;
  253.  
  254.         }
  255.  
  256.         case keyDown:
  257.         case autoKey:
  258.             key = theEvent->message & charCodeMask;
  259.             if (theEvent->modifiers & cmdKey)
  260.                 if (theEvent->what == keyDown)
  261.                     DoMenuCommand(MenuKey(key));
  262.             break;
  263.  
  264.  
  265.         case kOSEvent:
  266.             switch ((unsigned long) theEvent->message >> 24) {    /**  high byte of message  **/
  267.                  case kSuspendResumeMessage:            /**  suspend/resume is also an activate/deactivate  **/
  268.                     if ((theEvent->message & kResumeMask) == 0)
  269.                        gSleep = 80;                    /** we are headed to the background, so slow down...  **/
  270.                     else
  271.                     {
  272.                        gSleep = 0;                    /** we are headed to the foreground, so speed up...  **/
  273.  
  274. // On a resume event, we need to call GXUpdateJob on all of our documents'
  275. // jobs.  This is important because the user may have just changed
  276. // something which affects our jobs (like the size of the paper in the
  277. // printer).
  278. //
  279. // Since our application stores our document references in the refCon fields
  280. // of our documents' windows, we just loop through every one of our windows,
  281. // extract our document pointers and update the associated jobs.
  282.  
  283.                         whichWindow = FrontWindow();
  284.                         
  285.                         while (whichWindow != nil)
  286.                         {
  287.                             if (((WindowPeek) whichWindow)->windowKind == userKind)
  288.                                 GXUpdateJob(GetDocJob(whichWindow));
  289.                             
  290.                             whichWindow = (WindowPtr) ((WindowPeek) whichWindow)->nextWindow;
  291.                         }
  292.                     }
  293.                 break;
  294.             }
  295.         break;
  296.     }
  297. }
  298.  
  299.